package com.example.composestudy.part6

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import com.example.composestudy.R
import com.example.composestudy.utils.initialPage
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState

@Preview(showBackground = true)
@OptIn(ExperimentalPagerApi::class)
@Composable
fun DrawImage() {
    val pageCount = 2
    val initialPage = 1.initialPage()
    val state = rememberPagerState(initialPage)
    HorizontalPager(count = pageCount, modifier = Modifier.fillMaxWidth(), state = state) { page ->
        when (page) {
            0 -> DrawImage1()
            1 -> DrawImage2()
        }
    }
}

@Composable
fun DrawImage1() {
    PageItem(text = "DrawImage1") {
        // 方法一
        /*val context = LocalContext.current
        val bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.ic_launcher_foreground)
        val image = bitmap.asImageBitmap()*/
        val image = ImageBitmap.imageResource(id = R.drawable.avatar)
        Canvas(modifier = Modifier.fillMaxSize()) {
            drawImage(image = image)
        }
    }
}

/** 设置图片大小 */
@Composable
fun DrawImage2() {
    PageItem(text = "DrawImage2") {
        val image = ImageBitmap.imageResource(id = R.drawable.avatar)
        Canvas(modifier = Modifier.fillMaxSize()) {
            drawImage(
                image = image,
                // 这两个设置原图片的
                srcOffset = IntOffset(0, 0),
                srcSize = IntSize(100, 100),
                // 这两个设置目标图片的
                dstOffset = IntOffset(100, 100),
                dstSize = IntSize(800, 800)
            )
        }
    }
}
